package com.yixing.tech.system.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.yixing.tech.common.base.*;
import com.yixing.tech.common.enums.ComparisonSymbol;
import com.yixing.tech.system.dao.DictDao;
import com.yixing.tech.system.po.Dict;
import com.yixing.tech.system.service.IDictService;
import com.yixing.tech.common.utils.CommonUtil;
import com.yixing.tech.common.utils.DataUtil;
import com.yixing.tech.common.vo.s3.DictVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@Service
@Slf4j
public class DictServiceImpl implements IDictService {
    @Resource
    private BaseMongoDao baseMongoDao;
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private DictDao dictDao;

    @Value("${official-tenant-id}")
    private String officialTenantId;

    @Override
    public void add(DictVO dictVO) throws BusinessException {
        Dict dict = DataUtil.o2o(dictVO, Dict.class);
        dict.setId(null);
        baseMongoDao.save(dict);

    }

    @Override
    public void batchAdd(List<DictVO> list) throws BusinessException {

    }

    @Override
    public DictVO queryById(String id) {
        Dict dict = baseMongoDao.get(id, Dict.class);
        return DataUtil.o2o(dict, DictVO.class);
    }

    @Override
    public List<DictVO> queryByIds(List<String> ids) {
        return null;
    }

    @Override
    public DictVO queryByCondition(DictVO dictVO) throws BusinessException {
        return null;
    }

    @Override
    public void update(DictVO dictVO) throws BusinessException {
        baseMongoDao.update(DataUtil.o2o(dictVO, Dict.class));
    }

    @Override
    public Page<DictVO> list(PageRow<DictVO> pageRow) {
        DictVO condition = pageRow.getCondition();
        String parentId = condition.getParentId();
        if (StringUtils.isEmpty(parentId)) {
            condition.putComparisonSymbols("parentId", ComparisonSymbol.isNull);
        }
        condition.putComparisonSymbols("tenantId", ComparisonSymbol.in);
        List<Object> paramList = new ArrayList<>();
        paramList.add(condition.getTenantId());
        paramList.add(officialTenantId);
        condition.putParamsCondition("tenantId", paramList);
        condition.setSortCondition(new BaseVO.Sort("sort","asc"));
        Page<DictVO> page = baseMongoDao.listPage(pageRow, Dict.class, DictVO.class);
        List<DictVO> list = page.getData();
        list.forEach(m -> m.setIsPrivate(m.getTenantId().equals(condition.getTenantId())));
        findChildren(list,condition.getTenantId());
        return page;
    }

    @Override
    public List<DictVO> list(DictVO condition) {
        String parentId = condition.getParentId();
        if (StringUtils.isEmpty(parentId)) {
            condition.putComparisonSymbols("parentId", ComparisonSymbol.isNull);
        }
        condition.putComparisonSymbols("tenantId", ComparisonSymbol.in);
        List<Object> paramList = new ArrayList<>();
        paramList.add(condition.getTenantId());
        paramList.add(officialTenantId);
        condition.putParamsCondition("tenantId", paramList);
        condition.setSortCondition(new BaseVO.Sort("sort","asc"));
        List<DictVO> list = baseMongoDao.list(condition, Dict.class, DictVO.class);
        list.forEach(m -> m.setIsPrivate(m.getTenantId().equals(condition.getTenantId())));
        findChildren(list,condition.getTenantId());
        return list;
    }

    private void findChildren(List<DictVO> parent,String tenantId) {
        for (DictVO vo : parent) {
            if (!CommonUtil.listIsBlank(vo.getChildren())) {
                DictVO cond = new DictVO();
                cond.setParentId(vo.getId());
                cond.setTenantId(tenantId);
                vo.setChildren(list(cond));
            }

        }
    }


    @Override
    public void deleteById(String id) throws BusinessException {
        baseMongoDao.delete(id, Dict.class);
        mongoTemplate.remove(Query.query(Criteria.where("parentId").is(id)), Dict.class);
    }

    @Override
    public void delete(List<String> ids) throws BusinessException {

    }

    @Override
    public void addChild(DictVO vo) throws BusinessException {
        dictDao.addChild(vo);
    }

    @Override
    public void delete(DictVO vo) throws BusinessException {
        deleteById(vo.getId());
        String parentId = vo.getParentId();
        if (!StringUtils.isEmpty(parentId)) {
            dictDao.deleteChild(parentId, vo.getId());
        }
    }
}
